package com.lqfan.autoAnswerQ.dao.wrongSubject;

import com.lqfan.autoAnswerQ.model.quesBank.Question;
import com.lqfan.autoAnswerQ.model.wrongSubject.WrongSubject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zml
 * @since 2020-04-25
 */
@Mapper
public interface WrongSubjectMapper extends BaseMapper<WrongSubject> {

    /**
     * 做错题则插入错题在默认路径下
     * @param wrongSubject //获取更新默认次数会进行统一修改
     * @return
     */
    @Insert("insert into wrongSubject (id,questionId,userId,subjectId,confirmNumber,isDeleteWrong,path,name) values(#{id}," +
            "#{questionId},#{userId},#{subjectId},#{confirmNumber},#{isDeleteWrong},#{path},#{name})")
    boolean insertWrongSubject(WrongSubject wrongSubject);

    /**
     * 检查错题是否存在
     */
    @Select("select id from wrongSubject where userId =#{userId} and subjectId =#{subjectId}")
    String checkWrongSubject(String userId ,String subjectId);

    /**
     * 检查错题是否存在此路劲
     */
    @Select("select * from wrongSubject where userId =#{userId} and path =#{path}")
    WrongSubject checkWrongPath(String userId ,String path);
    /**
     * 每次做的时候进行比对
     * 若答题对得次数相等则删除错题
     */
    @Select("select id from wrongSubject where confirmNumber<=runNumber and userId =#{userId} and subjectId =#{subjectId}")
    String checkRunNum(String userId,String subjectId);

    /**
     * 每做一次进行记录
     * 修改目前答题次数
     */
    @Update("update wrongSubject set runNumber=runNumber+1 where userId =#{userId} and subjectId =#{subjectId}")
    boolean updateDoNum(String userId,String subjectId);

    /**
     * 每做一次进行记录
     * 修改目前答题次数
     */
    @Update("update wrongSubject set runNumber=0 where userId =#{userId} and subjectId =#{subjectId}")
    boolean updateDoNumInit(String userId,String subjectId);
    /**
     * 设置修改操作
     * 更新该用户所有错题得完成错题设置
     * 设置是否允许删除题库 整体更新
     * @param confirmNumber
     * @param userId
     * @return
     */
    @Update("update wrongSubject set confirmNumber=#{confirmNumber}, isDeleteWrong = #{isDeleteWrong} where userId =#{userId}")
    boolean updateWrongSet(String confirmNumber,String userId,Integer isDeleteWrong);

    /**
     * 手动删除指定错误题库,需要同时删除文件
     */
    @Delete("delete from wrongSubject where  userId =#{userId} and questionId = #{productId} ")
    boolean deleteWrongProduct(String userId,String productId);

    /**
     * 自动比对达到数据直接删除题目
     */
    @Delete("delete from wrongSubject where  userId =#{userId} and subjectId = #{subjectId} and isDeleteWrong =0")
    boolean deleteWrongSubject(String userId,String subjectId);


    /**
     * 获取对应路径下得所有错误题库
     */
    @Delete("delete from wrongSubject where userId =#{userId} and path like concat('%', #{path,jdbcType=VARCHAR}, '%')")
    boolean deleteWrongPath(String userId,String path);

    /**
     * 获取对应路劲下得所有错题题库
     * 获取文件名，文件类型，题目id，
     */
    @Select("select q.* from fileType f,wrongSubject w,question q where f.type =3 " +
            "and w.path=f.filePath and f.pid =#{pid} and f.type=3 and w.questionId = q.id and w.userId=q.userId and w.userId=f.userId " +
            "and w.userId=#{userId} group by q.id" )
    List<Question> getWrongQuestion(String pid, String userId);



}
